//
//  BrowseController.m
//  MyTrip
//
//  Created by zhang on 15/9/4.
//  Copyright (c) 2015年 张益. All rights reserved.
//

#import "BrowseController.h"
#import "UIImageView+WebCache.h"

@interface BrowseController ()

@property (nonatomic, retain) UIImageView *image;
@property (nonatomic, retain) UIImagePickerController *picker;


@end

@implementation BrowseController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor whiteColor]];
    CGFloat width = [[[_dic objectForKey:@"photo_info"] objectForKey:@"w"] integerValue];
    CGFloat height = [[[_dic objectForKey:@"photo_info"] objectForKey:@"h"] integerValue];
    
    CGFloat imageHeight = self.view.frame.size.width * height / width;
    
    self.image = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, imageHeight)] autorelease];
    [self.image sd_setImageWithURL:[NSURL URLWithString:[_dic objectForKey:@"photo_s"]] placeholderImage:[UIImage imageNamed:@"zhanweitu.jpg"]];
    self.image.center = self.view.center;
    self.image.center = CGPointMake(self.image.center.x, self.image.center.y - 64);
    [self.view addSubview:_image];
    
    
    UIButton *button = [[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 20)] autorelease];
    [button setImage:[UIImage imageNamed:@"baocun.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
}

- (void)buttonAction:(UIButton *)sender{
    self.picker = [[UIImagePickerController alloc] init];
    _picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    _picker.allowsEditing = YES;
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否保存" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        UIImageWriteToSavedPhotosAlbum(self.image.image, self, nil, nil);//回调的方法  回调方法可传的数据
        
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
    }];
    
    [alert addAction:ok];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
